package com.cr.admin.system.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.cr.admin.system.pojo.dto.SysUserPasswordParam;
import com.cr.admin.system.pojo.dto.SysUserProfileParam;
import com.cr.admin.system.pojo.vo.SysUserDetailVO;
import com.cr.admin.system.service.ISysUserService;
import com.cr.common.response.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

@Api(tags = "20.个人资料")
@RestController
@RequestMapping("/api/sys/users")
public class SysUserProfileController {

    @Autowired
    private ISysUserService sysUserService;

    @ApiOperation(value = "查询个人资料")
    @ApiOperationSupport(order = 500)
    @GetMapping("/profile")
    public JsonResult getProfile() {
        Long userId = StpUtil.getLoginIdAsLong();
        SysUserDetailVO sysUserDetailVO = sysUserService.selectProfile(userId);
        return JsonResult.success(sysUserDetailVO);
    }

    @ApiOperation(value = "修改个人资料")
    @ApiOperationSupport(order = 510)
    @PutMapping("/profile")
    public JsonResult updateProfile(@Validated @RequestBody SysUserProfileParam sysUserProfileParam) {
        Long userId = StpUtil.getLoginIdAsLong();
        sysUserService.updateProfile(userId, sysUserProfileParam);
        return JsonResult.success();
    }

    @ApiOperation(value = "修改个人密码")
    @ApiOperationSupport(order = 520)
    @PatchMapping("/profile/password")
    public JsonResult updatePassword(@Validated @RequestBody SysUserPasswordParam sysUserPasswordParam) {
        Long userId = StpUtil.getLoginIdAsLong();
        sysUserService.updatePassword(userId, sysUserPasswordParam);
        return JsonResult.success();
    }

    @ApiOperation(value = "修改个人头像")
    @ApiOperationSupport(order = 520)
    @PatchMapping("/profile/avatar")
    public JsonResult updateAvatar(@RequestParam("avatar") String avatar) {
        Long userId = StpUtil.getLoginIdAsLong();
        sysUserService.updateAvatar(userId, avatar);
        return JsonResult.success();
    }

}
